// pages/post/post.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        postList: [],
        content: '',
        postId: '',
        showCommentModal: false, // 控制弹框显示与隐藏  
        commentContent: '',
        accountId: app.accountInfo().accountId,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that = this;
        wx.request({
            url: app.rootUrl + '/getPosts',
            data: {
                userId: 'not'
            },
            header: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            method: 'POST',
            success: function (res) {
                that.setData({
                    postList: res.data.rows
                });
            }
        })
    },
    handleInput: function (e) {
        this.setData({
            content: e.detail.value
        });
    },
    publishPost() {
        let that = this
        if (app.accountInfo() == null) {
            wx.showToast({
                title: '请登陆',
                icon: 'none',
            })
            return null
        }
        if (!that.data.content) {
            wx.showToast({
                title: '请输入',
                icon: 'none',
            })
            return null
        }
        wx.request({
            url: app.rootUrl + '/createPost',
            data: {
                'userId': app.accountInfo().accountId,
                'postText': that.data.content
            },
            header: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            method: 'POST',
            success: function (res) {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                })
                that.onShow()
                that.setData({
                    content: ''
                });
            }
        })
    },
    handleCommentInput: function (e) {
        this.setData({
            commentContent: e.detail.value
        });
    },
    saveCommentPost() {
        let that = this
        if (!that.data.commentContent) {
            wx.showToast({
                title: '请输入',
                icon: 'none',
            })
            return null
        }
        wx.request({
            url: app.rootUrl + '/addComment',
            data: {
                'commentUserId': app.accountInfo().accountId,
                'commentPostId': that.data.postId,
                'commentText': that.data.commentContent,
                'commentParentId': '0'
            },
            header: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            method: 'POST',
            success: function (res) {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                })
                that.onShow()
                that.hideCommentModal()
            }
        })
    },
    showCommentModal: function (e) {
        if (app.accountInfo() == null) {
            wx.showToast({
                title: '请登陆',
                icon: 'none',
            })
            return null
        }
        const postId = e.currentTarget.dataset.postid; // 获取当前帖子的 ID  
        // 可以将 postId 存储到 data 中，以便后续提交评论时使用  
        this.setData({
            showCommentModal: true,
            commentContent: '',
            postId: postId
        });
    },

    // 隐藏评论弹框  
    hideCommentModal: function () {
        this.setData({
            showCommentModal: false
        });
    },
})